//
//  LLNavigationControllerStack.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLNavigationControllerStack.h"
#import "LLRouter.h"
#import "LLTabBarController.h"

@interface LLNavigationControllerStack ()
@property(nonatomic, strong) NSMutableArray *navigationControllers;
@end

@implementation LLNavigationControllerStack

+ (instancetype)sharedInstance {
    static LLNavigationControllerStack *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
        sharedInstance.navigationControllers = [[NSMutableArray alloc] init];
    });
    return sharedInstance;
}

- (void)pushNavigationController:(UINavigationController *)navigationController {
    if ([self.navigationControllers containsObject:navigationController]) return;
    [self.navigationControllers addObject:navigationController];
}

- (UINavigationController *)popNavigationController {
    UINavigationController *navigationController = self.navigationControllers.lastObject;
    [self.navigationControllers removeLastObject];
    return navigationController;
}

- (UINavigationController *)topNavigationController {
    return self.navigationControllers.lastObject;
}

- (void)pushViewModel:(LLViewModel *)viewModel animated:(BOOL)animated {
    UIViewController *viewController = (UIViewController *) [LLRouter.sharedInstance viewControllerForViewModel:viewModel];
    viewController.hidesBottomBarWhenPushed = YES;
    [self.navigationControllers.lastObject pushViewController:viewController animated:animated];
}

- (void)popViewModelAnimated:(BOOL)animated {
    [self.navigationControllers.lastObject popViewControllerAnimated:animated];
}

- (void)popToRootViewModelAnimated:(BOOL)animated {
    [self.navigationControllers.lastObject popToRootViewControllerAnimated:animated];
}

- (void)presentViewModel:(LLViewModel *)viewModel animated:(BOOL)animated completion:(VoidBlock)completion {
    UIViewController *viewController = (UIViewController *) [LLRouter.sharedInstance viewControllerForViewModel:viewModel];

    UINavigationController *presentingViewController = self.navigationControllers.lastObject;
    if (![viewController isKindOfClass:UINavigationController.class]) {
        viewController = [[UINavigationController alloc] initWithRootViewController:viewController];
    }
    [self pushNavigationController:(UINavigationController *) viewController];

    [presentingViewController presentViewController:viewController animated:animated completion:completion];
}

- (void)dismissViewModelAnimated:(BOOL)animated completion:(VoidBlock)completion {
    [self popNavigationController];
    [self.navigationControllers.lastObject dismissViewControllerAnimated:animated completion:completion];
}

- (void)resetRootViewModel:(LLViewModel *)viewModel {
    [self.navigationControllers removeAllObjects];

    UIViewController *viewController = (UIViewController *) [LLRouter.sharedInstance viewControllerForViewModel:viewModel];

    if (![viewController isKindOfClass:[UINavigationController class]] && ![viewController isKindOfClass:[LLTabBarController class]]) {
        viewController = [[UINavigationController alloc] initWithRootViewController:viewController];
        [self pushNavigationController:(UINavigationController *) viewController];
    }

    LLSharedAppDelegate.window.rootViewController = viewController;
}

@end
